package com.atguigu.guli.service.edu.controller.admin;

import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javafx.scene.chart.ValueAxis;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Date 2021/5/18 13:01
 * @Description TODO
 */
@RestController //@RestController 相当于@Controller+@ResponseBody
@RequestMapping("/admin/edu/teacher")
@Api(tags = "讲师管理模块") //描述controller
public class AdminTeacherController {

    @Autowired
    private TeacherService teacherService;

    //1、查询所有的讲师
    @ApiOperation(value = "查询所有讲师")
    @GetMapping("list")   //get请求没有请求体
    public R listAll() {
        List<Teacher> list = teacherService.list();
        return R.ok().setData("items", list);
    }


    //2、根据id查询讲师
    @ApiOperation(value = "根据id查询讲师")
    @GetMapping("getById/{id}")
    public R getById(@ApiParam(required = true,value = "用户id",defaultValue = "1") @PathVariable String id){
        Teacher teacher = teacherService.getById(id);
        return R.ok().setData("item",teacher);
    }

    //3、删除讲师
    @ApiOperation(value = "删除讲师")
    @DeleteMapping("removeById/{id}")
    public R removeById(@PathVariable String id){
        boolean b = teacherService.removeById(id);
        if (b) {
            return R.ok();
        }
        return R.error().setMessage("记录不存在");
    }

    //4、新增讲师
    @ApiOperation(value = "新增讲师")
    @PostMapping("save")
    public R save(@RequestBody Teacher teacher){
        boolean save = teacherService.save(teacher);
        if (save) {
            return R.ok();
        }
        return R.error().setData("item", "新增讲师失败");
    }

    //5、更新讲师
    @ApiOperation(value = "更新讲师")
    @PutMapping("update")
    public R update(@RequestBody Teacher teacher){
        boolean result = teacherService.updateById(teacher);
        if (result) {
            return R.ok().setMessage("修改成功");
        }else {
            return R.ok().setMessage("数据不存在，修改失败");
        }
    }

    //6、没有条件的分页查询
    @ApiOperation("没有条件的分页查询")
    @GetMapping("list/{pageNum}/{pageSize}")
    public R listPage(@PathVariable Integer pageNum,@PathVariable Integer pageSize){
        Page<Teacher> page = new Page<>(pageNum, pageSize);
         page = teacherService.page(page);
         return R.ok().setData("page",page);
    }

    //7、带条件的分页查询






    /*
            //1、查询所有讲师
            @GetMapping("list")
            public List<Teacher> listAll(){
                return teacherService.list();
            }
    */


}
